﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RevClinic.Core.Domain
{
    public class UserRole : Entity<long>
    {
        public virtual string Name { get; set; }

        public static readonly string Delimiter = "/";

        public UserRole() { }

        public UserRole(string roleName) : this(0, roleName) { }

        public UserRole(long id, string roleName)
        {
            Check.Argument.IsNotNullOrWhiteSpace(roleName, "roleName");
            Check.Argument.IsAcceptable(roleName, "roleName", name => {
                string delimiter = UserRole.Delimiter;
                bool notEndOrEqualDelimiter = !name.EndsWith(delimiter) || name.Equals(delimiter);
                return name.StartsWith(delimiter) && notEndOrEqualDelimiter;
            });
            Name = roleName;
        }

        public virtual bool IsChildOf(UserRole parentRole)
        {
            return Name.StartsWith(parentRole.Name + UserRole.Delimiter);
        }

        public virtual bool IsParentOf(UserRole childRole)
        {
            return childRole.IsChildOf(this);
        }
    }
}
